package com.example.core.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.core.entity.DealerForm;
import com.example.core.service.DatabaseService;
import com.example.core.service.ProcessingService;
import com.example.core.thread.AfterSaleThread;
import com.example.core.thread.GlobalThread;
import com.example.core.thread.NewCarThread;
import com.example.core.thread.UsedThread;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@RestController
public class ProcessingController {

    @Autowired
    ProcessingService processingService;

    @Autowired
    DatabaseService databaseService;

    @RequestMapping("/processing")
    public R processing(@Param("yearId") String yearId, @Param("quarterId") String quarterId) throws Exception {

        Integer rows = processingService.checkSyncData(yearId, quarterId);
        if (rows == null||rows == 0 ){
            return R.ok("复核表该季度无数据").setCode(100);
        }
        try {
            //基础数据
            Map<String, List<DealerForm>> baseMap = processingService.getBaseForm(yearId, quarterId);

            //指标计算
            ExecutorService executorService = Executors.newFixedThreadPool(8);

            NewCarThread newCarThread = new NewCarThread(yearId, quarterId, baseMap);
            UsedThread usedThread = new UsedThread(yearId, quarterId, baseMap);
            AfterSaleThread afterSaleThread = new AfterSaleThread(yearId, quarterId, baseMap);
            GlobalThread globalThread = new GlobalThread(yearId, quarterId, baseMap);

            Future<Map<String, Object>> newCarTask = executorService.submit(newCarThread);
            Future<Map<String, Object>> usedTask = executorService.submit(usedThread);
            Future<Map<String, Object>> afterSaleTask = executorService.submit(afterSaleThread);
            Future<Map<String, Object>> globalTask = executorService.submit(globalThread);

            Map<String, Object> carMap = newCarTask.get();
            Map<String, Object> saleMap = afterSaleTask.get();
            Map<String, Object> globalMap = globalTask.get();
            Map<String, Object> usedMap = usedTask.get();

            executorService.shutdown();


            List<DealerForm> dealerList = baseMap.get("currentDealer");
            List<DealerForm> regionDealerCount = Arrays.asList(new DealerForm(), new DealerForm(), new DealerForm(), new DealerForm(), new DealerForm());
            baseMap.get("regionDealerCount").stream().filter(o -> o.getQuarterId().equals(quarterId)).collect(Collectors.toList()).forEach(x -> {
                switch (x.getRegionName()) {
                    case "东区":
                        regionDealerCount.set(0, x);
                        break;
                    case "南区":
                        regionDealerCount.set(1, x);
                        break;
                    case "西区":
                        regionDealerCount.set(2, x);
                        break;
                    case "北区":
                        regionDealerCount.set(3, x);
                        break;
    //                case "中区":
    //                    regionDealerCount.set(4, x);
    //                    break;
                    default:
                }
            });

            //插库  计算方法，所有都是，因为调试问题注释掉部分，正式部署的时候不要忘记打开

            databaseService.handleJxsRepTotalInfo(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepKpiTtl(yearId, quarterId, dealerList, regionDealerCount, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepKpiNcs(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepKpiAs(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsGrossProfit(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsTotalInfo(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsGrossPro(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsOutputGross(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsOutputGross19(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsOutputNum(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAsOutputRateTax(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepNcsGrossProfit(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAvrcarCost(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepNcsTotalInfo(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepNcsGrossInfo(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepNcsGrossPro(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepNcsDerMange(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepUcTotalInfo(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepUcRevenueGross(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepAvrevCostRatio(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepProfitAnal(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsRepProfitDistribute(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);
            databaseService.handleJxsWriteStatis(yearId, quarterId, dealerList, carMap, usedMap, saleMap, globalMap);

        } catch (Exception e){
            processingService.insertJxsWrExceLog(e.getClass().toString(),e.getMessage(),yearId, quarterId);
            return R.failed("计算失败");
        }

        return R.ok("Success");
    }


}
